package com.learn.mvc.controller;

import com.learn.mvc.bean.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;

@Controller
public class ParamController {

    /**
     * 通过ServletAPI获取请求参数
     *
     * @param request
     * @return
     */
    @RequestMapping("/testParam_a")
    public String testParam_a(HttpServletRequest request) throws UnsupportedEncodingException {
        request.setCharacterEncoding("utf-8");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
//        String[] hobbies = request.getParameterValues("hobby"); // 多个同名
        System.out.println("username:" + username + ",password:" + password);
        return "success";
    }

    /**
     * 测试通过控制器方法的形参获取请求参数
     *
     * @param username
     * @param password
     * @return
     */
    @RequestMapping("/testParam_b")
    public String testParam_b(String username, String password) {
        System.out.println("username:" + username + ",password:" + password);
        return "success";
    }

    /**
     * 测试通过控制器方法的形参获取请求参数
     * 如果有同名的请求参数，在方法的形参位置设置字符串接收此同名参数
     * <p>
     * http://localhost:8080/springmvc/testParam_c?username=admin&password=admin&hobby=a&hobby=b&hobby=c
     * 输出 username:admin,password:admin,hobby:a,b,c
     *
     * @param username
     * @param password
     * @param hobby
     * @return
     */
    @RequestMapping("/testParam_c")
    public String testParam_c(String username, String password, String hobby) {
        System.out.println("username:" + username + ",password:" + password + ",hobby:" + hobby);
        return "success";
    }

    /**
     * 测试通过控制器方法的形参获取请求参数
     * 如果有同名的请求参数，在方法的形参位置设置字符串数组接收此同名参数
     * http://localhost:8080/springmvc/testParam_d?username=admin&password=admin&hobby=a&hobby=b&hobby=c
     * 输出 username:admin,password:admin,hobby:[a, b, c]
     *
     * @param username
     * @param password
     * @param hobby
     * @return
     */
    @RequestMapping("/testParam_d")
    public String testParam_d(String username, String password, String[] hobby) {
        System.out.println("username:" + username + ",password:" + password + ",hobby:" + Arrays.toString(hobby));
        return "success";
    }


    /**
     * @param username
     * @param password
     * @param hobby
     * @return
     * @RequestParam 请求参数和控制器方法的形参创建映射关系
     */
    @RequestMapping("/testParam_e")
    public String testParam_e(@RequestParam(value = "user_name", required = false, defaultValue = "默认名") String username,
                              @RequestParam("password") String password,
                              String[] hobby) {
        System.out.println("username:" + username + ",password:" + password + ",hobby:" + Arrays.toString(hobby));
        return "success";
    }

    /**
     * @param username
     * @param password
     * @param host
     * @return
     * @RequestHeader 测试从请求头中获取指定字段
     */
    @RequestMapping("/testParam_f")
    public String testParam_f(String username, String password, @RequestHeader(value = "hehe", required = true, defaultValue = "hehe") String host) {
        System.out.println("username:" + username + ",password:" + password + ",Host:" + host);
        return "success";
    }

    /**
     * 试创建session，触发cookie回调
     *
     * @param request
     * @return
     */
    @RequestMapping("/testParam_j")
    public String testParam_j(HttpServletRequest request) {
        HttpSession session = request.getSession(); // 创建session，触发cookie回调
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        System.out.println("username:" + username + ",password:" + password);
        return "success";
    }

    /**
     * 测试服务端打印cookie信息
     *
     * @param username
     * @param password
     * @param hobby
     * @param JSESSIONID
     * @return
     */
    @RequestMapping("/testParam_h")
    public String testParam_h(String username, String password, String[] hobby, @CookieValue("JSESSIONID") String JSESSIONID) {
        System.out.println("username:" + username + ",password:" + password + ",hobby:" + Arrays.toString(hobby));
        System.out.println(String.format("当前获取的JSESSIONID为%s", JSESSIONID));
        return "success";
    }


    /**
     * 通过POJO获取请求参数
     * @param username
     * @param password
     * @param user
     * @param user2
     * @return
     */
    @RequestMapping("/testParam_i")
    public String testParam_i(@SessionAttribute(value = "session_username")String username, String password, User user, User user2) {
        System.out.println("username:" + username + ",password:" + password);
        System.out.println(user);
        System.out.println(user2);
        return "success";
    }

}
